﻿using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using Inaction.Business.Security;
using System;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.MenuManage
{
    public partial class MenuManageForm : BaseForm
    {
        #region 属性
        MenuInfo CurrentMenu
        {
            get { return menuListBindingSource.Current as MenuInfo; }
        }

        RoleInfoList Roles
        {
            get { return roleInfoListBindingSource.List as RoleInfoList; }
        }
        #endregion

        #region 构造
        public MenuManageForm()
        {
            InitializeComponent();
            menuInfoBindingInfo.Criteria = new MenuCriteria { AppType = AppType.WinForm };
        }
        #endregion

        #region 事件
        private void TargetButtonEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.DefaultExt = "dll";
            dialog.Title = "选择dll文件";
            dialog.Filter = "dll|*.dll";
            dialog.InitialDirectory = Application.StartupPath;
            dialog.Multiselect = false;
            dialog.ShowDialog();
            if (string.IsNullOrEmpty(dialog.FileName)) return;
            var types = Inaction.Util.TypeHelper.GetSubClassType<Form>(dialog.FileName);
            var f = new TypeSelecteForm(types);
            f.ShowDialog();
            if (f.SelectedType != null)
            {
                CurrentMenu.TargetObj = f.SelectedType.Assembly.GetName().Name + ";" + f.SelectedType.FullName;
            }
        }


        private void checkedListBox_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            if (CurrentMenu == null || inRefresh || e.Index < 0) return;
            var role = checkedListBox.GetItem(e.Index) as RoleInfo;
            role.Selected = e.State == CheckState.Checked;
            CurrentMenu.RefreshMenuRole(role);
        }

        private void barManager_FormRulesApplied(object sender, EventArgs e)
        {
            checkedListBox.Enabled = barManager.EditMode;
            ShowMenuRoles();
        }

        #endregion 

        #region 方法
        bool inRefresh = false;
        private void ShowMenuRoles()
        {
            if (Roles == null) return;
            inRefresh = true;
            if (CurrentMenu == null)
            {
                checkedListBox.UnCheckAll();
                foreach (var role in Roles) role.Selected = false;
            }
            else
            {
                for (int i = 0; i < Roles.Count; i++)
                {
                    var role = Roles[i];
                    role.Selected = CurrentMenu.MenuRoles.Any(p => p.RoleId == role.Id);
                    checkedListBox.SetItemChecked(i, role.Selected);
                }
            }
            inRefresh = false;
        }
        #endregion
    }
}